package org.primefaces.examples.mobile;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.primefaces.event.FileUploadEvent;
import org.primefaces.examples.domain.DroppedFile;
import org.primefaces.examples.view.TableBean;
import org.primefaces.model.UploadedFile;

@ManagedBean
@ViewScoped
public class DropFileMessage implements Serializable{
	
	public static final String TXT_TYPE = "txt";
	public static final String PDF_TYPE = "pdf";
	public static final String XLS_TYPE = "xls";
	public static final String PPT_TYPE = "ppt";
	public static final String DOC_TYPE = "doc";
	public static final String DOCX_TYPE = "docx";
	public static final String ZIP_TYPE = "zip";
	public static final String RAR_TYPE = "rar";
	public static final String UNKNOWN_TYPE = "unknown";
	
	
	
	
	public DropFileMessage() {
			
	}
	
	@ManagedProperty("#{dropFileAdmin}")
	private DropFileAdmin dropFileAdmin;
	
	@ManagedProperty("#{tableBean}")
	private TableBean tableBean;
	
	private String to;

	private String subject;
	
	private String message;
	
	@ManagedProperty("#{dropFileAdmin.returnRecipt}")
	private boolean returnRecipt;

	@ManagedProperty("#{dropFileAdmin.public}")
	private boolean publicMessage;
	
	@ManagedProperty("#{dropFileAdmin.deleteAfterDownload}")
	private boolean deleteAfterDownload;
	
	@ManagedProperty("#{dropFileAdmin.defaultExpiryDate}")
	private Date expiryDate;
	
	
	 private UploadedFile file;

	 private boolean sending = false;
	   

	    public void upload() {
	        FacesMessage msg = new FacesMessage("Succesful", file.getFileName() + " is uploaded.");
			FacesContext.getCurrentInstance().addMessage(null, msg);
	    }

	    public void handleFileUpload(FileUploadEvent event) {
			FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
			FacesContext.getCurrentInstance().addMessage(null, msg);
		}
	
	    public String send() {
	        
	    	sending = true;
			
			try {
				//Simulate long running action
				Thread.sleep(3000);
				
				
				if(Math.random() > 0.8)
				{
					FacesMessage msg = new FacesMessage("Error", "Don't worry! This is a random generated error! Relax..");
					FacesContext.getCurrentInstance().addMessage(null, msg);
					sending = false;
					return ""; //RANDOM ERROR
				}
				
				DroppedFile droppedFile = new DroppedFile();
				droppedFile.setSenderEmail(dropFileAdmin.getEmail());
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat("ddMMHHMM");
				droppedFile.setFileName(file.getFileName());
				droppedFile.setTxId(simpleDateFormat.format(new Date()));
				droppedFile.setRecipientEmail(to);
				droppedFile.setType(getIconTypeForFile(file.getFileName()));
				
				tableBean.getSentFiles().add(droppedFile);
				
				FacesMessage msg = new FacesMessage("Succesful", "File sent!");
				FacesContext.getCurrentInstance().addMessage(null, msg);
				
			} catch (InterruptedException e) {
				
				e.printStackTrace();
			}catch (Throwable e) {
				FacesMessage msg = new FacesMessage("Error", "Problem sending file");
				FacesContext.getCurrentInstance().addMessage(null, msg);
				
			}
			
			
			sending = false;
			
			return "pm:fileSent";
	    	
	    }
	    
    public UploadedFile getFile() {
        return file;
    }

    public void setFile(UploadedFile file) {
        this.file = file;
    }
	    
	    
	public boolean isReturnRecipt() {
		return returnRecipt;
	}

	public void setReturnRecipt(boolean returnRecipt) {
		this.returnRecipt = returnRecipt;
	}


	public boolean isDeleteAfterDownload() {
		return deleteAfterDownload;
	}

	public void setDeleteAfterDownload(boolean deleteAfterDownload) {
		this.deleteAfterDownload = deleteAfterDownload;
	}

	
	
	public void setTo(String to) {
		System.out.println(new Date() + to);
		this.to = to;
	}

	public String getTo() {
		return to;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getMessage() {
		return message;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getSubject() {
		return subject;
	}

	public void setExpiryDate(Date expiryDate) {
		this.expiryDate = expiryDate;
	}

	public Date getExpiryDate() {
		return expiryDate;
	}

	public void setSending(boolean sending) {
		this.sending = sending;
	}

	public boolean isSending() {
		return sending;
	}

	public void setDropFileAdmin(DropFileAdmin dropFileAdmin) {
		this.dropFileAdmin = dropFileAdmin;
	}

	public DropFileAdmin getDropFileAdmin() {
		return dropFileAdmin;
	}

	public void setTableBean(TableBean tableBean) {
		this.tableBean = tableBean;
	}

	public TableBean getTableBean() {
		return tableBean;
	}
	
	
	public static String getIconTypeForFile(String fileName)
	{
		if(fileName.endsWith(TXT_TYPE))
			return TXT_TYPE;
		if(fileName.endsWith(DOC_TYPE) || fileName.endsWith(DOCX_TYPE))
			return DOC_TYPE;
		if(fileName.endsWith(ZIP_TYPE) || fileName.endsWith(RAR_TYPE))
			return "archive";
		if(fileName.endsWith(PDF_TYPE))
			return PDF_TYPE;
		if(fileName.endsWith(XLS_TYPE))
			return XLS_TYPE;
		if(fileName.endsWith(PPT_TYPE))
			return PPT_TYPE;
		
		
		
		return UNKNOWN_TYPE; //DEFAULT
		
		
	}

	public void setPublicMessage(boolean publicMessage) {
		this.publicMessage = publicMessage;
	}

	public boolean isPublicMessage() {
		return publicMessage;
	}
}
